/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistentie;

import domein.Day;
import domein.DomeinController;
import domein.Episode;
import domein.Serie;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;

/**
 *
 * @author vuokko
 */
class SerieMapper {

    /*
     * public Serie getSerie(String title) { Serie s = null; Document d =
     * PersistentieController.getInstance().getXMLFromFile(); for (Object o :
     * d.getRootElement().getChildren()) { Element e = (Element)o; if
     * ((e).getChild("title").getText().equals(title)) { List<Episode> l =
     * PersistentieController.getInstance().getEpisodes(e); s = new
     * Serie(e.getChildText("title"), l,
     * Day.valueOf(e.getChildText("airdate"))); } } return s; }
     *
     */
    public List<Serie> getAllSeries() {
        ArrayList<Serie> l = new ArrayList<>();
        Document d = PersistentieController.getInstance().getXMLFromFile();
        for (Object o : d.getRootElement().getChildren()) {
            Element e = (Element) o;
            List<Episode> el = PersistentieController.getInstance().getEpisodes(e);
            l.add(new Serie(e.getChildText("title"), el, Day.valueOf(e.getChildText("airdate"))));
        }
        return l;
    }
    
    void saveAllSeries() {
        List<Serie> l = DomeinController.getInstance().getSeries();
        Document d = new Document(new Element("series"));
        for (Serie s : l) {
            Element serie = new Element("serie");
            serie.addContent(new Element("title").setText(s.getTitle()));
            serie.addContent(new Element("airdate").setText(s.getAirDay().name())); 
            serie.addContent(PersistentieController.getInstance().getEpisodesAsElement(s.getEpisodes()));
            d.getRootElement().addContent(serie);
        }
        PersistentieController.getInstance().saveAllToXml(d);
    }
}
